package org.example.libraryhelper;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class BookListContentHandler extends DefaultHandler {

	private String TagName=null;
	private LoanBookInfo book=null;
	private List<LoanBookInfo> books=null;
	private boolean isInsideRowSet=false;
	private boolean isInReaderName=false;
	private String ReaderName=null;
	
	public String GetReaderName()
	{
		return ReaderName;
	}
	
	public BookListContentHandler(List<LoanBookInfo> books)
	{
		super();
		this.books=books;
	}
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	
		String tmp = new String(ch, start, length);
		if(tmp.trim().length()==0) return;
		if(isInReaderName) ReaderName=tmp;
		if(isInsideRowSet)
		{
			if(TagName.equals("ISBN"))
				book.ISBN=tmp;
			if(TagName.equals("TITLE"))
				book.BookName=tmp;
			if(TagName.equals("AUTHOR"))
				book.BookAuthor=tmp;
			if(TagName.equals("BARCODE"))
				book.BarCode=tmp;
			if(TagName.equals("STATE"))
				book.BookState=tmp;
			if(TagName.equals("CALLNO"))
				book.CallNum=tmp;
			if(TagName.equals("CIRTYPE"))
				book.BookTypeCode=tmp;
			if(TagName.equals("CURLOCAL"))
				book.HoldingPlace=tmp;
			if(TagName.equals("LOANCOUNT"))				
				book.LoanCount=2-Integer.parseInt(tmp);
			if(TagName.equals("LOANDATE"))
				book.LoanDate=tmp;
			if(TagName.equals("RETURNDATE"))				
			{	
				book.ReturnDate=tmp;
				book.calcDaysRemain();
			}
		}
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equals("ROWSET2")) 
			isInsideRowSet=false;

		if(localName.equals("RDNAME"))
			isInReaderName=false;
		
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
	 */
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub

		super.startDocument();
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		if(localName.equals("RDNAME"))
			isInReaderName=true;
			
		if(localName.equals("ROWSET2")) 
			isInsideRowSet=true;
		
		if(isInsideRowSet)
		{
			TagName=localName;
			if(localName.equals("ROW"))			
			{			
				book=new LoanBookInfo();
				books.add(book);
			}
		}
		else
		{
			TagName=localName;
		}
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
	}

}
